

package cn.iocoder.yudao.module.apijson.framework;

import apijson.RequestMethod;
import apijson.orm.*;
import apijson.orm.model.Access;
import apijson.orm.model.Document;
import apijson.orm.model.Function;
import apijson.orm.model.Request;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;


/**
 * SQL配置
 * TiDB 用法和 MySQL 一致
 *
 * @author Lemon
 */
public class APIJSONSQLConfig extends AbstractSQLConfig<Long, JSONObject, JSONArray> {
    public static final String TAG = "APIJSONSQLConfig";

    public static boolean ENABLE_COLUMN_CONFIG = false;

    public static String USER_ID_KEY = "user_id";

    public static Callback<Long, JSONObject, JSONArray> SIMPLE_CALLBACK;

    static {
        DEFAULT_DATABASE = DATABASE_MYSQL;  //TODO 默认数据库类型，改成你自己的
        DEFAULT_SCHEMA = "";  //TODO 默认模式名，改成你自己的，默认情况是 MySQL: sys, PostgreSQL: public, SQL Server: dbo, Oracle:
        //  由 APIJSONVerifier.init 方法读取数据库 Access 表来替代手动输入配置
        TABLE_KEY_MAP.put(Access.class.getSimpleName(), "apijson_access");
        TABLE_KEY_MAP.put(Function.class.getSimpleName(), "apijson_function");
        TABLE_KEY_MAP.put(Document.class.getSimpleName(), "apijson_document");
        TABLE_KEY_MAP.put(Request.class.getSimpleName(), "apijson_request");

        SIMPLE_CALLBACK = new SimpleCallback<Long, JSONObject, JSONArray>() {
            @Override
            public SQLConfig<Long, JSONObject, JSONArray> getSQLConfig(
                    RequestMethod method, String database, String schema, String datasource, String table) {
                SQLConfig<Long, JSONObject, JSONArray> config = APIJSONApplication.createSQLConfig();
                config.setMethod(method);
                config.setDatabase(database);
                config.setDatasource(datasource);
                config.setSchema(schema);
                config.setTable(table);
                return config;
            }
            @Override
            public String getUserIdKey(String database, String schema, String datasource, String table) {
                return USER_ID_KEY; // id / userId
            }

            //取消注释来实现数据库自增 id
            @Override
            public Long newId(RequestMethod method, String database, String schema, String datasource, String table) {
                return null; // return null 则不生成 id，一般用于数据库自增 id
            }
        };

    }

    /**
     * 获取SQL配置
     */
    public static SQLConfig<Long, JSONObject, JSONArray> newSQLConfig(
            RequestMethod method, String table, String alias, JSONObject request, List<Join<Long, JSONObject, JSONArray>> joinList, boolean isProcedure) throws Exception {
        return newSQLConfig(method, table, alias, request, joinList, isProcedure, SIMPLE_CALLBACK);
    }

    public APIJSONSQLConfig() {
        this(RequestMethod.GET);
    }

    public APIJSONSQLConfig(RequestMethod method) {
        super(method);
    }

    public APIJSONSQLConfig(RequestMethod method, String table) {
        super(method, table);
    }

    public APIJSONSQLConfig(RequestMethod method, int count, int page) {
        super(method, count, page);
    }


    public String gainDBVersion() {
        return null;
    }

    public String gainDBUri() {
        return null;
    }

    public String gainDBAccount() {
        return null;
    }

    public String gainDBPassword() {
        return null;
    }

    /**
     * 获取 APIJSON 配置表所在数据库模式 database，默认与业务表一块
     */
    public String getConfigDatabase() {
        return getDatabase();
    }

    /**
     * 获取 APIJSON 配置表所在数据库模式 schema，默认与业务表一块
     */
    public String getConfigSchema() {
        return getSchema();
    }

    /**
     * 是否为 APIJSON 配置表，如果和业务表一块，可以重写这个方法，固定 return false 来提高性能
     */
    public boolean isConfigTable() {
        return CONFIG_TABLE_LIST.contains(getTable());
    }

    @Override
    public String gainSQLDatabase() {
        String db = isConfigTable() ? getConfigDatabase() : super.gainSQLDatabase();
        return db == null ? DEFAULT_DATABASE : db;
    }

    @Override
    public String gainSQLSchema() {
        String sch = isConfigTable() ? getConfigSchema() : super.gainSQLSchema();
        return sch == null ? DEFAULT_SCHEMA : sch;
    }

    /**
     * 是否开启逻辑删除
     */
    @Override
    public boolean isFakeDelete() {
        return true;
    }
    @Override
    public String getUserIdKey() {
        return USER_ID_KEY;
    }


    // 支持 !key 反选字段 和 字段名映射，依赖插件 https://github.com/APIJSON/apijson-column
    @Override
    public APIJSONSQLConfig setColumn(List<String> column) {
        if (ENABLE_COLUMN_CONFIG) {
            column = ColumnUtil.compatInputColumn(column, getTable(), getMethod(), getVersion(), !isConfigTable());
        }
        super.setColumn(column);
        return this;
    }

    @Override
    public String gainKey(String key) {
        if (ENABLE_COLUMN_CONFIG) {
            key = ColumnUtil.compatInputKey(key, getTable(), getMethod());
        }
        return super.gainKey(key);
    }


}
